/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.slice;

import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.app.utill.PixelMapUtil;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.filters.ColorFilter;
import com.lxj.xpopup.util.ToastUtil;

/**
 * 彩色滤镜效果
 *
 * @since 2021-05-18
 */
public class ColorFilterAbilitySlice extends BaseAbilitySlice {
    private static final int ONETWO = 12;
    private static final int FIVE = 5;
    private static final int FOURFIVE = 45;
    private static final int TWOZERO = 20;
    private static final int ONEFIVE = 15;
    private String[] colorStyles = new String[ONETWO];
    private DirectionalLayout directionalLayout;
    private Image image1;

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_color_filter;
    }

    @Override
    protected void initComponents(Intent intent) {
        initView();
    }

    private void initView() {
        directionalLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_linear);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        initColorStyles();
        CV4JImage cv4Jimage1 = new CV4JImage(PixelMapUtil.getPixelMapFromResource(
            getContext(), ResourceTable.Media_test_color_filter).get());
        ColorFilter colorFilter1 = new ColorFilter();
        colorFilter1.setStyle(0);
        image1.setPixelMap(colorFilter1.filter(cv4Jimage1.getProcessor()).get().getImage().toBitmap());
        for (int i1 = 0; i1 < colorStyles.length; i1++) {
            DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout
                .LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT);

            DirectionalLayout directionalLayout1 = new DirectionalLayout(this);
            directionalLayout1.setAlignment(LayoutAlignment.CENTER);
            directionalLayout1.setOrientation(Component.HORIZONTAL);
            directionalLayout1.setTag(i1);
            directionalLayout1.setLayoutConfig(layoutConfig);

            DirectionalLayout.LayoutConfig textLayoutConfig = new DirectionalLayout.LayoutConfig(
                DirectionalLayout.LayoutConfig.MATCH_CONTENT,
                DirectionalLayout.LayoutConfig.MATCH_CONTENT);
            textLayoutConfig.alignment = LayoutAlignment.CENTER;

            Text text = new Text(this);
            text.setText(colorStyles[i1]);
            text.setTextAlignment(TextAlignment.CENTER);
            text.setTextSize(AttrHelper.fp2px(ONEFIVE, getContext()));
            text.setLayoutConfig(textLayoutConfig);
            directionalLayout1.addComponent(text);
            directionalLayout.addComponent(directionalLayout1);
            directionalLayout1.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    CV4JImage cv4Jimage = new CV4JImage(PixelMapUtil.getPixelMapFromResource(getContext(),
                        ResourceTable.Media_test_color_filter).get());
                    ColorFilter colorFilter = new ColorFilter();
                    colorFilter.setStyle((int) directionalLayout1.getTag());
                    image1.setPixelMap(colorFilter.filter(cv4Jimage.getProcessor()).get().getImage().toBitmap());
                    ToastUtil.showToast(getContext(), colorStyles[(int) directionalLayout1.getTag()]);
                }
            });
        }
    }

    private void initColorStyles() {
        colorStyles[ColorFilter.AUTUMN_STYLE] = " 秋天风格 ";
        colorStyles[ColorFilter.BONE_STYLE] = " 硬朗风格 ";
        colorStyles[ColorFilter.COOL_STYLE] = " 凉爽风格 ";
        colorStyles[ColorFilter.HOT_STYLE] = " 热带风格 ";
        colorStyles[ColorFilter.HSV_STYLE] = " 色彩空间变换风格 ";
        colorStyles[ColorFilter.JET_STYLE] = " 高亮风格 ";
        colorStyles[ColorFilter.OCEAN_STYLE] = " 海洋风格 ";
        colorStyles[ColorFilter.PINK_STYLE] = " 粉色风格 ";
        colorStyles[ColorFilter.RAINBOW_STYLE] = " 彩虹风格 ";
        colorStyles[ColorFilter.SPRING_STYLE] = " 春天风格 ";
        colorStyles[ColorFilter.SUMMER_STYLE] = " 夏天风格 ";
        colorStyles[ColorFilter.WINTER_STYLE] = " 冬天风格 ";
    }
}

